package com.flame.shapeville.component.task3;

import javafx.concurrent.Worker;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;
import javafx.scene.web.WebView;

/**
 * Represents a triangle used in Task 3.
 * <p>
 * This class draws a triangle with customizable base and height,
 * and provides a detailed view with formula explanation.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-14
 */
public class JfTriangle extends JfShape {

    /**
     * Base length of the triangle.
     */
    private int base;

    /**
     * Height of the triangle from base to apex.
     */
    private int height;

    /**
     * Constructs a new triangle with specified dimensions.
     *
     * @param size the width/height of the drawing area
     * @param base the base length of the triangle
     * @param height the height of the triangle
     */
    public JfTriangle(int size, int base, int height) {
        super();
        this.base = base;
        this.height = height;

        this.graphics = loadShape(size, centerX, centerY);
        shape.getChildren().add(graphics);
        initDetailed(size);
    }

    /**
     * Loads the visual representation of the triangle into the provided pane.
     *
     * @param size the width/height of the drawing area
     * @param x X-coordinate center of the shape
     * @param y Y-coordinate center of the shape
     * @return the Shape object representing the triangle
     */
    public Shape loadShape(int size, double x, double y) {
        int multiple = Math.min(size / base, size / height);
        Shape shape = new Polygon(
                (double) size / 2 - (double) (base * multiple) / 2, size,
                (double) size / 2 + (double) (base * multiple) / 2, size,
                (double) size / 2, size - height * multiple
        );
        initBaseParams(shape);
        shape.setLayoutX(x - (double) (base * multiple) / 2);
        return shape;
    }

    /**
     * Initializes the detailed view of the triangle.
     * Includes formula explanation using an embedded web view.
     *
     * @param graphicsSize the size used for rendering the detailed shape
     */
    @Override
    void initDetailed(int graphicsSize) {
        Label dataLabel = new Label("base = " + base + "\nheight = " + height);
        dataLabel.setLayoutX(50);
        dataLabel.setLayoutY(10);

        Shape detailedGraphics = loadShape(graphicsSize, centerX - this.shape.getPrefWidth() / 4, centerY);

        Line dashedLine = new Line();
        dashedLine.setStartX(0);
        dashedLine.setStartY(20);
        dashedLine.setEndX(0);
        dashedLine.setEndY(220);
        dashedLine.setStroke(Color.GRAY);
        dashedLine.setStrokeWidth(2);
        dashedLine.getStrokeDashArray().addAll(10d, 4d);
        dashedLine.setTranslateX(centerX - 1);

        WebView webView = new WebView();
        webView.setPrefSize(220, 240);
        webView.setLayoutX(centerX + 1);
        webView.setStyle("-fx-background-color: transparent;");
        String url = getClass().getResource("/static/formula/task3/triangle.html").toExternalForm();
        webView.getEngine().load(url);
        webView.getEngine().getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                webView.getEngine().executeScript("updateFormula(" + base + "," + height + ")");
            }
        });

        detailedShape.getChildren().addAll(dataLabel, detailedGraphics, dashedLine, webView);
    }
}
